package edu.njust.cma.util;

import edu.njust.cma.DTO.Excel04;
import edu.njust.cma.DTO.Excel05;
import edu.njust.cma.pojo.Equipment;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;

public class ExcelUtil05 {
    // 生成Excel文件的方法
    public static XSSFWorkbook createExcelFile(List<Excel05> dataList) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Data");

        // 创建单元格样式并设置边框
        XSSFCellStyle borderStyle = workbook.createCellStyle();
        borderStyle.setBorderTop(BorderStyle.THIN);
        borderStyle.setBorderBottom(BorderStyle.THIN);
        borderStyle.setBorderLeft(BorderStyle.THIN);
        borderStyle.setBorderRight(BorderStyle.THIN);

        // 创建表头
        XSSFRow headerRow1 = sheet.createRow(0);

        headerRow1.createCell(0).setCellValue("序号");
        headerRow1.createCell(1).setCellValue("类别(产品/项目/参数)");
        headerRow1.createCell(2).setCellValue("产品/项目/参数");
        headerRow1.createCell(4).setCellValue("依据的标准（方法）");
        headerRow1.createCell(6).setCellValue("仪器设备（标准物质）");
        headerRow1.createCell(10).setCellValue("溯源方式");
        headerRow1.createCell(11).setCellValue("有效日期");
        headerRow1.createCell(12).setCellValue("确认结果");

        XSSFRow headerRow2 = sheet.createRow(1);
        headerRow2.createCell(2).setCellValue("序号");
        headerRow2.createCell(3).setCellValue("名称");
        headerRow2.createCell(4).setCellValue("名称");
        headerRow2.createCell(5).setCellValue("编号（含年号）");
        headerRow2.createCell(6).setCellValue("编号");
        headerRow2.createCell(7).setCellValue("名称");
        headerRow2.createCell(8).setCellValue("型号/规格/等级");
        headerRow2.createCell(9).setCellValue("测量范围");

        // 添加表头
        XSSFRow headerRow3 = sheet.createRow(2);

        // 合并表头单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 5));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 6, 9));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 10, 10));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 11, 11));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 12, 12));

        //填充数据
        int rowNum = 2;
        for (Excel05 data : dataList) {
            XSSFRow row = sheet.createRow(rowNum++);
            //第一行留给序号
            row.createCell(1).setCellValue(data.getMinorCategoryName());
            row.createCell(3).setCellValue(data.getTaskName());
            //System.out.println(data.getTaskName());
            row.createCell(4).setCellValue(data.getStandardName());
            row.createCell(5).setCellValue(data.getStandardNumber());
            row.createCell(6).setCellValue(data.getEquipmentNumber());
            row.createCell(7).setCellValue(data.getEquipmentName());
            row.createCell(8).setCellValue(data.getDescription());
            row.createCell(9).setCellValue(data.getMeasuringRange());
            row.createCell(10).setCellValue(data.getTraceway());
            row.createCell(11).setCellValue(data.getExpiryTime());
        }
        return workbook;
    }
}
